using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.CompositeUI.Utility;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace Jbe.TestSuite.LogViewer
{
    [SmartPart]
    public partial class LogView : UserControl, ILogView
    {
        private LogViewPresenter presenter;

        [CreateNew]
        public LogViewPresenter Presenter
        {
            set
            {
                presenter = value;
                presenter.View = this;
                UpdateStartStopButtons();
            }
        }

        public LogView()
        {
            InitializeComponent();

            logEntryBindingSource.DataSource = LogRecorder.LogEntries;

            LogRecorder.LogEntryAdded += new EventHandler<DataEventArgs<LogEntry>>(LogRecordChanged);
            LogRecorder.LogCleared += new EventHandler(LogRecordChanged);
        }

        protected override void OnLoad(EventArgs e)
        {
            presenter.OnViewReady();
        }

        private void LogRecordChanged(object sender, EventArgs e)
        {
            logEntryBindingSource.ResetBindings(false);
            if (dataGridView.Rows.Count > 0)
            {
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[0];
                dataGridView.CurrentCell = null;
            }
        }

        private void StartButtonClick(object sender, EventArgs e)
        {
            presenter.RecorderEnabled = true;
            UpdateStartStopButtons();
        }

        private void StopButtonClick(object sender, EventArgs e)
        {
            presenter.RecorderEnabled = false;
            UpdateStartStopButtons();
        }

        private void ClearButtonClick(object sender, EventArgs e)
        {
            presenter.Clear();
        }

        private void UpdateStartStopButtons()
        {
            stopButton.Enabled = presenter.RecorderEnabled;
            startButton.Enabled = !presenter.RecorderEnabled;
        }

        private void DataGridViewDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            entriesLabel.Text = dataGridView.Rows.Count + " Entries";
        }

        private void DataGridViewCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value is ICollection<string>) 
            {
                StringBuilder builder = new StringBuilder();
                foreach(string str in (ICollection<string>)e.Value) {
                    builder.Append(str);
                    builder.Append(", ");
                }
                if (builder.Length >= 2) { builder.Remove(builder.Length - 2, 2); }
                e.Value = builder.ToString();
                e.FormattingApplied = true;
            }
        }
    }
}